package com.lemon.tuvi2015;

import org.json.JSONException;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.tuvi2015.object.GiachuObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;
import com.lemon.tuvi2015.view.PagerSlidingTabStrip;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class XongdatDetailsActivity extends FragmentActivity {
	CustomActionbar actionbar;
	String cmd;
	ImageView imageViewGiachu;
	TextView textGiachu,textDuonglich,textAmlich,textNguhanh,textTuoi;
	ProgressBar progressBar;
	View main;
	private PagerSlidingTabStrip tabs;
	ViewPager viewPager;
	ViewpagerAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xongdat_details);

		initView();
		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			cmd = extras.getString(TagName.KEY);
		
		showContent(cmd);
		initActionbar();
	}

	void initView() {
		main=findViewById(R.id.main);
		imageViewGiachu=(ImageView) findViewById(R.id.imageViewGiachu);
		textGiachu=(TextView) findViewById(R.id.textViewGiachu);
		textAmlich=(TextView) findViewById(R.id.textViewSinham);
		textDuonglich=(TextView) findViewById(R.id.textViewSinhduong);
		textNguhanh=(TextView) findViewById(R.id.textViewMenh);
		textTuoi=(TextView) findViewById(R.id.textViewTuoi);
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		OnClickListener listenerShare = new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				BaseEngine.shareBitmap(XongdatDetailsActivity.this,  BaseEngine.ConvertViewtoBitmap(XongdatDetailsActivity.this, main));
			}
		};
		actionbar.setActionbar(R.string.home_menu_xongdat, listenerBack, listenerShare);
	}

	private void showContent(String content) {
		try {
			GiachuObject giachuObject=JsonParser.readgiachu(content).get(0);
			if(giachuObject!=null){
				textGiachu.setText(giachuObject.getname());
				textAmlich.setText(giachuObject.getamlich());
				textDuonglich.setText(giachuObject.getduonglich());
				textTuoi.setText(giachuObject.gettuoi());
				textNguhanh.setText(giachuObject.getnguhanh());
				
				Picasso.with(XongdatDetailsActivity.this).load(giachuObject.getavatar()).into(imageViewGiachu, new Callback() {
					@Override
					public void onSuccess() {
						progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onError() {
					}
				});
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		adapter = new ViewpagerAdapter(getSupportFragmentManager());
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(0);
		tabs.setViewPager(viewPager);
		
//		final String mimeType = "text/html";
//		final String encoding = "utf-8";
//		String data = Html.fromHtml(detailsObject.getcontent()).toString();
//		webView.loadDataWithBaseURL(null, data, mimeType, encoding, null);
	}
	
	public class ViewpagerAdapter extends FragmentStatePagerAdapter {
		public ViewpagerAdapter(FragmentManager fm) {
			super(fm);
		}

		String[] listType = { getResources().getString(R.string.xongdat_tuoixung)
				, getResources().getString(R.string.xongdat_tuoikhac)};

		@Override
		public CharSequence getPageTitle(int position) {
			return listType[position];
		}

		@Override
		public int getCount() {
			return listType.length;
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public Fragment getItem(int i) {
			switch (i) {
			case 1:
				return new XongdatListFragment(cmd, TagName.TUOI_KHAC);
			default:
				return new XongdatListFragment(cmd, TagName.TUOI_XUNG);
//				return new XongdatTime(cmd);
			}
		}
	}
}
